
var prefix = ctx+"/sys/role"
$(function() {
	load();
});
var tree;
function load() {
	var dataStr = $("#list").val();
	var datas = $.parseJSON( dataStr );
	layui.use(['tree', 'util'], function() {
		tree = layui.tree,
			layer = layui.layer,
			util = layui.util
			//模拟数据
			,
			data = datas;
		//基本演示
		tree.render({
			elem: '#tree',
			data: data,
			showCheckbox: true, //是否显示复选框
			id: 'demoId1',
			isJump: false, //是否允许点击节点时弹出新窗口跳转
			click: function(obj) {
				var data = obj.data; //获取当前点击的节点数据
				layer.msg('状态：' + obj.state + '<br>节点数据：' + JSON.stringify(data));
			}
		});

		//按钮事件
		util.event('lay-demo', {
			getChecked: function(othis) {
				var checkedData = tree.getChecked('demoId1'); //获取选中节点的数据

				layer.alert(JSON.stringify(checkedData), {
					shade: 0
				});
				console.log(checkedData);
			},
			setChecked: function() {
				tree.setChecked('demoId1', [12, 16]); //勾选指定节点
			},
			reload: function() {
				//重载实例
				tree.reload('demoId1', {

				});

			}
		});
	});
}
function save() {
	var checkedData = tree.getChecked('demoId1'); //获取选中节点的数据
	var id = getCheckedId(checkedData);
	var ids = id.split(",");
	var menuIds = $("#menuIds").val(ids);
	$.ajax({
		cache : true,
		type : "POST",
		url : ctx+"/sys/role/saveMenu",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});
}
function getCheckedId(jsonObj) {
	var id = "";
	$.each(jsonObj, function (index, item) {
		if (id != "") {
			id = id + "," + item.id;
		} else {
			id = item.id;
		}
		var i = getCheckedId(item.children);
		if (i != "") {
			id = id + "," + i;
		}
	});
	return id;
}
